﻿using HMACAuthentication.WebApi.Filters;
using YMJF_Service.Common;
using YMJF_Service.Models;
using PetaPoco;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;

namespace YMJF_Service.Controllers
{
    /// <summary>
    /// 标签管理
    /// </summary>
    [RoutePrefix("api/tag")]
  
    public class TagController : ApiController
    {
        ConnectionStringMySQLDB db = new ConnectionStringMySQLDB();

        /// <summary>
        /// 标签列表
        /// </summary>
        [HttpGet]
        [Route("list")]
        public ApiResult GetTagList(long page, long pagesize, string sort, string order, string name)
        {
            ApiResult res = new ApiResult();
            try
            {
                string sqlwhere = "";
                if (!string.IsNullOrEmpty(name))
                {
                    sqlwhere += string.Format(" and name like '%{0}%' ", name);
                }
                string sqlorder = " order by " + sort + " " + order;
                Page<Tag> list = Tag.Page(page, pagesize, @"SELECT * FROM [dbo].[Tag] where SysDeleted = 0 " + sqlwhere + sqlorder);
                res.code = HttpCode.success;
                res.message = "获取标签列表成功";
                res.data = list;
            }
            catch (Exception ex)
            {
                res.code = HttpCode.fail;
                res.message = ex.Message;
            }
            return res;
        }

        /// <summary>
        /// 获取标签
        /// </summary>
        [HttpGet]
        [Route("get")]
        public ApiResult GetTag(string Id)
        {
            ApiResult res = new ApiResult();
            try
            {
                Tag tag = Tag.SingleOrDefault(" where SysDeleted = 0 and ID = @0 ", Id);
                if (tag != null)
                {
                    res.code = HttpCode.success;
                    res.message = "获取标签成功";
                    res.data = tag;
                }
                else
                {
                    res.code = HttpCode.Que_Failed;
                    res.message = "获取标签失败";
                }

            }
            catch (Exception ex)
            {
                res.code = HttpCode.fail;
                res.message = ex.Message;
            }
            return res;
        }

        /// <summary>
        /// 添加标签
        /// </summary>
        [HttpPost]
        [Route("add")]
        [HMACAuthenticationAttribute]
        public ApiResult AddTag(Tag data)
        {
            ApiResult res = new ApiResult();

            try
            {
                string user = "admin";

                data.ID = Guid.NewGuid().ToString();
                //data.SysOrder = 9999;
                data.SysCreatedBy = user;
                data.SysCreate = DateTime.Now;
                data.SysLastUpdBy = user;
                data.SysLastUpd = DateTime.Now;
                data.SysDeleted = false;
                object obj = data.Insert();
                if (obj != null)
                {
                    res.code = HttpCode.success;
                    res.message = "添加成功";
                }
                else
                {
                    res.code = HttpCode.Add_Failed;
                    res.message = "添加失败";
                }

            }
            catch (Exception ex)
            {
                res.code = HttpCode.fail;
                res.message = ex.Message;
            }
            return res;

        }

        /// <summary>
        /// 修改标签
        /// </summary>
        [HttpPost]
        [Route("update")]
        [HMACAuthenticationAttribute]
        public ApiResult UpdateTag(Tag tag)
        {
            ApiResult res = new ApiResult();

            try
            {
                string user = "admin";
                Tag tag2 =  Tag.SingleOrDefault(" where ID = @0 ",tag.ID);
                tag2.Name = tag.Name;
                tag2.remark = tag.remark;
                tag2.SysLastUpd = DateTime.Now;
                tag2.SysLastUpdBy = user;

                int obj = tag2.Update();

                if (obj > 0)
                {
                    res.code = HttpCode.success;
                    res.message = "修改成功";
                }
                else
                {
                    res.code = HttpCode.Add_Failed;
                    res.message = "修改失败";
                }

            }
            catch (Exception ex)
            {
                res.code = HttpCode.fail;
                res.message = ex.Message;
            }
            return res;

        }

        /// <summary>
        /// 删除标签
        /// </summary>
        [HttpPost]
        [Route("del")]
        [HMACAuthenticationAttribute]
        public ApiResult DelTag(string ID)
        {
            ApiResult res = new ApiResult();

            try
            {
               string user = "admin";
               int obj =  Tag.Update(string.Format(" set SysDeleted = 1,SysLastUpd = '{1}',SysLastUpdBy = '{2}' where ID = '{0}' ", ID,DateTime.Now,user));               
                if (obj >0)
                {
                    res.code = HttpCode.success;
                    res.message = "删除成功";
                }
                else
                {
                    res.code = HttpCode.Add_Failed;
                    res.message = "删除失败";
                }

            }
            catch (Exception ex)
            {
                res.code = HttpCode.fail;
                res.message = ex.Message;
            }
            return res;

        }

    }
}
